<?php

class EmailsService {

	public $dbutil;
	
	function __construct($dbutil){
		$this->dbutil =  $dbutil;
	}
	
	 public function getEmailsByEmail($email) {
        $sql = "SELECT * from emails WHERE  email = '".$email."'";
        $result = $this->dbutil->get_results ( $sql );
        if(count($result) > 0){
        	return $result[0];
        }else{
        	return null;
        }
    }
    
    public function addEmail($email,$email_origin){
    	$sql = "insert into emails (email,email_origin,create_date) values ('".$email."','".$email_origin."',sysdate())";
		return	$this->dbutil->query($sql);
	}
  	
	public function addEmails($data,$format=null){
		return	$this->dbutil->insert("emails", $data,$format);
	}
	
	public function edit($data,$conditions, $format=null){
		return $this->dbutil->update("emails",$data,$conditions,$format);
	}
	
	public function emailsPage($start , $page_size ){
		$limit ="";
		if($page_size){
			$limit =" limit $start,$page_size ";
		}
		$sql = " select id,email,email_origin,user_name ,DATE_FORMAT(create_date ,'%Y-%m-%d %H:%i:%s') create_date,post_code ,country from emails $limit";
		return $this->dbutil->get_results($sql);
	}
		
	public function deleteEmail($data){
		$sql = " delete from emails where id= $data";
		return $this->dbutil->get_results($sql);
	}
	public function countNum(){
		$sql = "select count(id) countnum from emails";
    	$resutrnarry=	$this->dbutil->get_results($sql);
    	return $resutrnarry[0]->countnum;
	}
}
?>